package com.yc.bdw.Widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import java.io.IOException;
import java.net.URL;

public class MyImageView extends ImageView {

	Handler mHandler;
	public MyImageView(final Context context) {
		super(context);
		mHandler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					Bitmap bitmap = (Bitmap) msg.obj;
					setImageBitmap(bitmap);
					break;
				default:
					break;
				}
			}};
			
	}
	public MyImageView(final Context context,AttributeSet attr) {
		super(context,attr);
		mHandler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					Bitmap bitmap = (Bitmap) msg.obj;
					setImageBitmap(bitmap);
					break;
				default:
					break;
				}
			}};
			
	}
	
	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
	}
	
	@Override
	public void setLayoutParams(LayoutParams params) {
		super.setLayoutParams(params);
	}
	
	public void SetImageURLAsync(final String url) {
		Thread thd=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					URL picUrl=new URL(url);
					Bitmap bmp=BitmapFactory.decodeStream(picUrl.openStream());
					Message msg=new Message();
					msg.what=1;
					msg.obj=bmp;
					mHandler.sendMessage(msg);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		thd.start();
	}
}
